/*
 * Created by JFormDesigner on Wed Mar 30 01:47:35 CST 2022
 */

package com.wangcanxuan.www.view.user;

import com.wangcanxuan.www.controller.Impl.NoticeControllerImpl;
import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.NoticeController;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.Notice;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 查看通知界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class CheckNoticeView extends JFrame {
    private final UserController userController;
    private final NoticeController noticeController;
    private final User user;
    private int noticePage = 1;
    private String chooseNoticeId;

    public CheckNoticeView(User user) {
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        noticeController = ControllerProxy.getProxyInstance(NoticeControllerImpl.class);
        this.user = user;
        initComponents();
        pageText.setText(String.valueOf(noticePage));
        fillTaskTable(0);
        setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }

    private boolean fillTaskTable(int pageCount) {
        ResultInfo<List<Notice>> listResultMessage = noticeController.getNotices(user.getId(), pageCount);
        List<Notice> notices = listResultMessage.getObj();
        String message = listResultMessage.getMessage();
        int code = listResultMessage.getCode();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
            // 设置成0行
            dtm.setRowCount(0);
            if (notices.size() == 0) {
                return true;
            }
            for (Notice notice : notices) {
                Vector<Object> list = new Vector<>();
                list.add(notice.getId());
                list.add(notice.getNoticeType());
                list.add(notice.getContent());
                list.add(notice.getCreateTime());
                dtm.addRow(list);
            }
            return true;
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            return false;
        }
        return false;
    }

    private void back(ActionEvent e) {
        // TODO add your code here
        new MainView(refreshUser());
        dispose();
    }

    private User refreshUser() {
        ResultInfo<User> refresh = userController.refresh(user.getId());
        return refresh.getObj();
    }

    private void upPage(ActionEvent e) {
        // TODO add your code here
        if (noticePage == 1) {
            JOptionPane.showMessageDialog(null, "这是首页！");
            return;
        }
        int count = (noticePage - 2) * 10;
        boolean b = fillTaskTable(count);
        if (b) {
            noticePage--;
            pageText.setText(String.valueOf(noticePage));
        }
    }

    private void downPage(ActionEvent e) {
        // TODO add your code here
        int count = noticePage * 10;
        boolean b = fillTaskTable(count);
        if (b) {
            noticePage++;
            pageText.setText(String.valueOf(noticePage));
        }
    }

    private void table1MousePressed(MouseEvent e) {
        // TODO add your code here
        int selectedRow = table1.getSelectedRow();
        chooseNoticeId = String.valueOf(table1.getValueAt(selectedRow, 0));
    }

    private void finishNotice(ActionEvent e) {
        if (chooseNoticeId == null) {
            JOptionPane.showMessageDialog(null, "请选择通知！");
            return;
        }
        // TODO add your code here
        ResultInfo<?> resultMessage = noticeController.finishNotice(chooseNoticeId);
        String message = resultMessage.getMessage();
        JOptionPane.showMessageDialog(null, message);
        new CheckNoticeView(refreshUser());
        dispose();

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        backButton = new JButton();
        label1 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        finishNoticeButton = new JButton();
        upPageButton = new JButton();
        pageText = new JTextField();
        downPageButton = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- backButton ----
        backButton.setText("\u8fd4\u56de\u4e3b\u9875");
        backButton.addActionListener(e -> back(e));

        //---- label1 ----
        label1.setText("\u901a\u77e5");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                    new Object[][]{
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                            {null, null, null, null},
                    },
                    new String[]{
                            "\u901a\u77e5id", "\u901a\u77e5\u7c7b\u522b", "\u901a\u77e5\u5185\u5bb9", "\u901a\u77e5\u65f6\u95f4"
                    }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    table1MousePressed(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- finishNoticeButton ----
        finishNoticeButton.setText("\u5df2\u9605");
        finishNoticeButton.addActionListener(e -> finishNotice(e));

        //---- upPageButton ----
        upPageButton.setText("\u4e0a\u4e00\u9875");
        upPageButton.addActionListener(e -> upPage(e));

        //---- pageText ----
        pageText.setEditable(false);

        //---- downPageButton ----
        downPageButton.setText("\u4e0b\u4e00\u9875");
        downPageButton.addActionListener(e -> downPage(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(122, 122, 122)
                                                .addComponent(upPageButton, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
                                                .addGap(69, 69, 69)
                                                .addComponent(pageText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(43, 43, 43)
                                                .addComponent(downPageButton, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(68, 68, 68)
                                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 486, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(32, 32, 32)
                                                .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
                                                .addGap(132, 132, 132)
                                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addGap(218, 218, 218)
                                                .addComponent(finishNoticeButton, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(99, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                                .addComponent(finishNoticeButton)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
                                .addGap(39, 39, 39)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(upPageButton)
                                        .addComponent(pageText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(downPageButton))
                                .addGap(53, 53, 53))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JButton backButton;
    private JLabel label1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton finishNoticeButton;
    private JButton upPageButton;
    private JTextField pageText;
    private JButton downPageButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
